<template>
  <div class="ele-body">
    <el-card shadow="never">
      <CustomSelect label="审核类型:" @input="reload" v-model="where.type" :select-options="auditTypeList"/>
      <CustomSelect label="关于我的:" @input="reload" v-model="where.about_me" :select-options="about_meList"/>
      <CustomSelect label="状态:" @input="reload" v-model="where.status" :select-options="statusList"/>
    </el-card>
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" class="ele-form-search" label-width="77px" @keyup.enter.native="reload"
               @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="搜索查询:">
              <el-input v-model="where.keyword" clearable placeholder="请输入关键词"/>
            </el-form-item>
          </el-col>
<!--          <el-col :lg="6" :md="12">-->
<!--            <el-form-item label="入库类别:">-->
<!--              <el-select v-model="where.status" class="ele-block" clearable placeholder="入库类别">-->
<!--                <el-option v-for="(item, index) in typeList" :key="index" :label="item" :value="item"/>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :lg="6" :md="12">
            <el-form-item label="仓库选择:">
              <el-autocomplete  v-model="warehouse_name" :fetch-suggestions="querySearch" class="w-full"
                               placeholder="仓库选择" value-key="name" value="id"  @select="handleSelectStock"></el-autocomplete>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button v-if="permission.includes('sys:bom:index')" class="ele-btn-icon" icon="el-icon-search"
                         type="primary" @click="reload">
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="url" :selection.sync="selection" :where="where"
                     height="calc(100vh - 462px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
        </template>
        <template slot="status_code" slot-scope="{row}">
          <el-tag
            :type="row.status_text !=='待办理'?(row.status_text ==='办理中'?'':(row.status_text ==='已拒绝'?'danger':(row.status_text ==='已撤销'?'info':(row.status_text ==='已退回'?'info':'success')))):'warning' "
            effect="dark">
<!--            {{ row.status_text !=='待办理'?row.status_text ==='办理中'?'办理中':'已办理':'待办理'}}-->
            {{ row.status_text}}
          </el-tag>
        </template> <template slot="items" slot-scope="{row}">
<!--          {{row.items[0].name}} / {{row.items[0].c_number}} / {{row.items[0].model}}-->
        {{ row.items.map(item=>{return item.name}).join(', ') }}
        </template>

        <template slot="bom_info" slot-scope="{row}">
          <el-tag
            effect="dark">
            {{ row.bom_info }}
          </el-tag>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-button type="text" v-if="permission.includes('sys:bom:audit')"   @click="audit(row)" :disabled=" row.status_text ==='已办理'||row.status_text ==='已拒绝'||!row.show_audit"
          >
            审核
          </el-button>
          <el-button type="text" v-if="permission.includes('sys:bom:reject')" slot="reference"  @click="reject(row.id)" :disabled="row.status_text ==='已办理'||row.status_text ==='已拒绝'||!row.show_refuse"
                   >
            拒绝
          </el-button>
          <el-button type="text" v-if="permission.includes('sys:bom:revoke')" :underline="false"   @click="revoke(row.id)" :disabled="row.status_text ==='已办理'||row.status_text ==='已拒绝'||!row.show_cancel"
          >
            撤销
          </el-button>

        </template>
      </ele-pro-table>
    </el-card>
    <audit ref="audit" @reload="reload"></audit>
    <revoke ref="revoke" @reload="reload"></revoke>
    <reject ref="reject" @reload="reload"></reject>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import audit from "@/views/bom/receipt/components/audit.vue";
import Revoke from "@/views/bom/receipt/components/revoke.vue";
import Reject from "@/views/bom/receipt/components/reject.vue";
import CustomSelect from "@/views/reports/workorder-monitoring/components/CustomSelect.vue";

export default {
  name: 'SystemLevel',
  components: {Reject, Revoke, audit,CustomSelect},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      flag: false,
      // 表格数据接口
      url: '/inventories_audit',
      // 表格列配置
      columns: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',
        //   fixed: "left"
        // },
        {
          prop: 'number',
          label: '编号',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'apply_date',
          label: '申请日期',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          slot: 'status_code',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'type_text',
          label: '申请类别',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
        },
        {
          slot: 'items',
          prop: 'items',
          label: '备件信息',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'quantity',
          label: '数量',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'un_complete_quantity',
          label: '待办数量',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'completed_quantity',
          label: '已办数量',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'target_warehouse_name',
          label: '目标仓库',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },  {
          prop: 'source_warehouse_name',
          label: '原仓库',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },

        {
          prop: 'create_user_name',
          label: '申请人',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'target_user_name',
          label: '办理人',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'action_time',
          label: '办理时间',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },

        {
          columnKey: 'action',
          label: '操作',
          width: 160,
          align: 'center',
          resizable: false,
          slot: 'action',
          emptyText: '暂无',
          fixed: "right"
        },
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false,
      warehouse_name:'',
      typeList:[],
      auditTypeList:[
        {label:'调拨',value:'allot'},
        {label:'分配',value:'allocation'},
        {label:'申领',value:'apply'},
        // {label:'退回',value:'back'}
      ],
      about_meList:[
        {label:'需要我审核的',value:'audit'},
        {label:'我申请的',value:'create'},
      ],
      statusList:[
        {label:'待办理',value:'pending'},
        {label:'办理中',value:'processing'},
        {label:'已办理',value:'processed'},
        {label:'已拒绝',value:'reject'},
        {label:'已撤销',value:'cancel'},
        // {label:'已退回',value:'back'}
      ]
    };
  },
  mounted() {
    this.getParams()
  },
  methods: {
    /*获取参数*/
    getParams(){
      this.$http.get(`/inventories/params`).then((res) => {
        if(res.data.code===0){
          this.typeList=res.data.data.storage_types
        }
      })
    },
    /*选择仓库*/
    handleSelectStock(val){
      console.log(val,this.where)
      this.where.warehouse_id = val.id
    },
    /*获取仓库列表*/
    querySearch(query,cb){
      query = query ? query : ''
      this.$http.get(`/warehouses?name=${query} `).then((res) => {
        cb(res.data.data)
      });
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.warehouse_name=''
      this.where = {};
      this.reload();
    },
    /*审核*/
    audit(info) {
      this.$refs.audit.active(info)
    },
    /*撤销*/
    revoke(info){
      this.$refs.revoke.active(info)
    },
    /*拒绝*/
    reject(id){
      this.$refs.reject.active(id)
    }
  }
}
</script>

<style scoped>
</style>
